// pages/user/userFList/userFList.js
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    listArr: [
      { "img": "../../../images/userIcon/shop.png", "title": "我的信息" },
      { "img": "../../../images/userIcon/shop.png", "title": "我的订单" },
      //{ "img": "../../../images/userIcon/youhui.png", "title": "店铺优惠" },
      //{ "img": "../../../images/userIcon/card.png", "title": "我的卡包" },
      //{ "img": "../../../images/userIcon/dongtai.png", "title": "已发的动态" },
      //{ "img": "../../../images/userIcon/love.png", "title": "收藏店铺" }
      ],
    listArr2: [
      { "img": "../../../images/userIcon/message.png", "title": "消息" }
      , { "img": "../../../images/userIcon/sao.png", "title": "联系客服" }
      //, { "img": "../../../images/userIcon/sao.png", "title": "扫码" }
      ],
    openId:'',
    userInfo:{},//用户信息
    isCard:-1,//默认没有名片

    imgURL: app.globalData.imgURL,//图片总路径

  },

  //生成图片分享
  makeCard:function(){
    var that = this;
    if (!that.data.userInfo) {
      wx.showToast({
        title: '请先登录',
        icon: 'none'
      })
      return;
    }

    app.post('card/sendCard', { openId: wx.getStorageSync('userInfo').userId }).then((res) => {
      console.log('success');
      console.log(res);
      if (res.code == 0) {
        var nArr = [app.globalData.makeCardURL + res.resultData];
        wx.previewImage({
          current: nArr[0],
          urls: nArr,
        })
      } else {
        wx.showModal({
          title: '温馨提示',
          content: res.msg,
          showCancel: false,
        })
      }
    }).catch((errMsg) => {
      console.log('fail');
      console.log(errMsg + '失败');
    })
  },
/*
   app.post(app.globalData.testURL + 'wx/getWxOpenId', { code: res.code }).then((res) =>{
      console.log('success');
      console.log(res);
    }).catch((errMsg) =>{
      console.log('fail');
      console.log(errMsg+'失败');
    })

*/

  onGotUserInfo: function (e) {
    var that = this;
    //判断是否登录过
    console.log(e)
    if (wx.getStorageSync('userInfo').userId){
      wx.showModal({
        title: '温馨提示',
        content: '是否更新信息？',
        success(resbtn) {
          if (resbtn.confirm) {
            console.log('用户点击确定')
            that.updateUserInfo(e);
          } 
        }
      })
    }else{
      that.updateUserInfo(e);
    }
  },

  updateUserInfo:function(e){
    var that = this;
    if (e.detail.errMsg == 'getUserInfo:fail auth deny') {
      wx.showModal({
        title: '温馨提示',
        content: '登录失败，请重试！',
      })
    } else {
      console.log('授权成功');
      app.post('muser/userLoginByWx', { openid: that.data.openId, username: e.detail.userInfo.nickName, gender: e.detail.userInfo.gender, user_icon: e.detail.userInfo.avatarUrl, city: e.detail.userInfo.city, province: e.detail.userInfo.province, }).then((res) => {
        console.log('请求成功');
        console.log(res);
        if (res.code == 0) {
          if (res.resultData.isCard == 0) {
            wx.setStorageSync('userInfo', {
              'userId': res.resultData.userInfo.openid,
              'user_icon': res.resultData.userInfo.user_icon,
              'username': res.resultData.userInfo.username,
              'isShopMsg': res.resultData.isShopMsg,
              'isCard': res.resultData.isCard,
              'isCustomer': res.resultData.isCustomer,
              'customerType': res.resultData.customerType,
              'cardMsg': {
                'slogan': res.resultData.cardInfo.slogan,
                'contactName': res.resultData.cardInfo.contact_name,
                'contactTel': res.resultData.cardInfo.contact_tel,
                'merName': res.resultData.cardInfo.mer_name,
                'merAddr': res.resultData.cardInfo.mer_addr,
                'variety': res.resultData.cardInfo.variety
              }
            });//保存用户基本信息
          } else {
            wx.setStorageSync('userInfo', {
              'userId': res.resultData.userInfo.openid,
              'user_icon': res.resultData.userInfo.user_icon,
              'username': res.resultData.userInfo.username,
              'isShopMsg': res.resultData.isShopMsg,
              'isCustomer': res.resultData.isCustomer,
              'isCard': res.resultData.isCard,
              'customerType': res.resultData.customerType,
            });//保存用户基本信息

          }
          that.setData({
            userInfo: wx.getStorageSync('userInfo'),
          })
          wx.navigateTo({
            url: '../userInfoEntry/userInfoEntry',
          })
          
        } else {
          wx.showModal({
            title: '温馨提示',
            content: res.msg,
            showCancel: false,
          })
        }
      }).catch((errMsg) => {
        console.log('请求失败');
      })
    }
  },

  chooseUserIcon:function(){
    console.log('修改icon');
  },

  //修改名片
  cardEditBtnClick:function(){
    console.log('修改名片');
    var that = this;
    if (!that.data.userInfo) {
      wx.showToast({
        title: '请先登录',
        icon: 'none'
      })
      return;
    }

    wx.navigateTo({
      url: '../userCard/userCard',
    })
  },

  //立即拥有名片
  getCard:function(){
    console.log('立即拥有名片');
    var that = this;
    if (!that.data.userInfo) {
      wx.showToast({
        title: '请先登录',
        icon: 'none'
      })
      return;
    }
    wx.navigateTo({
      url: '../userCard/userCard',
    })
  },

  //列表按钮点击
  fChoose:function(e){
    var that = this;
    console.log(e.currentTarget.id);
    var index = parseInt(e.currentTarget.id);

    if (!that.data.userInfo) {
      wx.showToast({
        title: '请先登录',
        icon:'none'
      })
      return;
    }


    switch(index){
      case 0:
        // app.post('shop/checkShop', { openId: wx.getStorageSync('userInfo').userId }).then((res) => {
        //   console.log('success');
        //   console.log(res);
        //   if(res.code == 0 ){
        //     wx.navigateTo({
        //       url: '../myShop/myShop',
        //     })
        //   }else if(res.code == 1){
        //     wx.showToast({
        //       title: res.msg,
        //       icon: 'none'
        //     })
        //   }else{
        //     wx.showModal({
        //       title: '温馨提示',
        //       content: res.msg,
        //       confirmText:'进入',
        //       success:function(result){
        //         if(result.confirm){
        //           console.log('点击确定')
        //           wx.navigateTo({
        //             url: '../myShop/myShop',
        //           })
        //         }
        //       }
        //     })
        //   }
        // }).catch((errMsg) => {
        //   console.log('fail');
        //   console.log(errMsg + '失败');
        // })
        wx.navigateTo({
          url: '../myInfo/myInfo',
            })
        break;
      case 1:
          // wx.navigateTo({
          //   url: '../cheapShop/cheapShop',
          // })
          wx.navigateTo({
            url: '../userOrders/userOrders',
          })
        break;
      case 2:
        wx.navigateTo({
          url: '../cardBag/cardBag',
        })
        break;
      case 3:
        wx.navigateTo({
          url: '../allDynamic/allDynamic',
        })
        break;
      case 4:
        wx.navigateTo({
          url: '../loveShop/loveShop',
        })
        break;
      case 5:
        wx.navigateTo({
          url: '../messages/messages',
        })
        break;
      case 6:
        wx.navigateTo({
          url: '../contactCustomerService/contactCustomerService',
        })
        // wx.scanCode({
        //   success(res) {
        //     console.log(res)
        //     that.useQR(res.result);//使用
        //   }
        // })
        break;
      default:
        console.log('default');
        break;
    }

  },

  //扫描二维码
  useQR:function(QRcontent){
    var that = this;
    if (QRcontent.indexOf('coupon_') == 0){
      app.post('coupon/useCoupon', {content:QRcontent}).then((res) => {
        console.log('success');
        console.log(res);
        wx.showModal({
          title: '温馨提示',
          content: res.msg,
          showCancel:false,
        })
      }).catch((errMsg) => {
        console.log('fail');
        console.log(errMsg + '失败');
      })
    }else{
      wx.showToast({
        title: '请扫描卡包二维码',
        icon:'none',
      })
    }
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      userInfo: wx.getStorageSync('userInfo'),//获取用户信息
    })
    console.log('that.data.userInfo');

    console.log(that.data.userInfo);
    if (!that.data.userInfo){
      console.log('没有数据');
      wx.login({
        success: function (res) {
          if (res.code) {
            app.post('wx/getWxOpenId', { code: res.code }).then((res) => {
              console.log('success');
              console.log(res.resultData);
              that.setData({
                openId: res.resultData.openId,
              })
            }).catch((errMsg) => {
              console.log('fail');
              console.log(errMsg + '失败');
            })
          } else {
            console.log('获取用户登录态失败！' + res.errMsg)
          }
        }
      })
    }else {
      that.setData({
        openId: wx.getStorageSync('userInfo').userId,
      })
    }  
  },

  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  //没有店铺的时候分享
  shareClick: function () {
    wx.showToast({
      title: '请先创建店铺',
      icon: 'none'
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    console.log('分享分享分享');
    return{
      title:'你好，这是我的名片，请惠存',
      path: 'pages/home/shopMessage/shopMessage?id=' + wx.getStorageSync('userInfo').userId +'&isShare=1',
      success:function(res){
        console.log("转发成功", res);
      },
      fail:function(res){
        console.log("转发失败", res);
      }

    }
  }
})